using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;

public partial class _Contact : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int ItemID = 0;

        if (Page.IsPostBack)
        {
            string MessageBody = "";
            MailAddress FromAddress;
            MailAddress ToAddress;
            MailMessage Message = new MailMessage();
            SmtpClient Client = new SmtpClient();
            NetworkCredential Credential = new NetworkCredential("jenn@jacceramics.com", "uxpigs65");
            
            try
            {
                // check that all required fields are filled in.
                if ((Name.Text == "") || (EmailAddress.Text == "") || (MessageBox.Text == ""))
                {
                    throw new Exception();
                }
            
                // set the TO and FROM addresses for the message
                
                FromAddress = new MailAddress("jenn@jacceramics.com");
                ToAddress = new MailAddress("jenn@jacceramics.com");

                // compile the message body
                MessageBody = CreateMessageBody();
                
                // configure the message
                Message.IsBodyHtml = true;
                Message.From = FromAddress;
                Message.To.Add(ToAddress);
                Message.Subject = "jacceramics.com Message";
                Message.Body = MessageBody;
                
                // configure the mail client
                Client.Host = "mail.jacceramics.com";
                Client.Port = 25;
                Client.UseDefaultCredentials = false;
                Client.Credentials = Credential;
                
                // use the client to send the message
                Client.Send(Message);
           
                // indicate successful send
                Instructions.Text = "Your message has been sent. I'll respond as soon as I can. Thank you!";
                Instructions.BackColor = System.Drawing.Color.Green;
                Instructions.ForeColor = System.Drawing.Color.White;
            }
            catch (Exception Ex)
            {
                //Instructions.Text = "We are sorry for the inconvenience, but an error has occured and your message was not sent.";
                Instructions.Text = Ex.Message;
                Instructions.BackColor = System.Drawing.Color.Maroon;
                Instructions.ForeColor = System.Drawing.Color.White;
                Trace.Warn(Ex.Message);
            }
            finally
            {
                Message.Dispose();
                Message = null;
                Client = null;
            }
        }
        else
        {
            // if an item id was passed, retrieve the item details and populate the messagebox
            if (Int32.TryParse(Request.QueryString["item"], out ItemID))
            {
                OleDbDataReader ItemDetails = DAL.GetItemDetails(ItemID);
                if (ItemDetails.HasRows)
                {
                    ItemDetails.Read();
                    MessageBox.Text = "I am interested in the piece \"" + ItemDetails.GetString(1) + "\". Please contact me with more information.";
                }
            }
        }
    }
    protected void SendMessage_Click(object sender, EventArgs e)
    {
        
    }
    protected string CreateMessageBody()
    {
        string MessageBody = "";
        MessageBody += "<html>";
        MessageBody += "    <head>";
        MessageBody += "        <title>A jacceramics.com Message</title>";
        MessageBody += "            <style type='text/css'>";
        MessageBody += "                .grid { table-layout: fixed; border-collapse: collapse; }";
        MessageBody += "                .col1 { width: 100px; padding: 3px; border: solid #EEEEEE 1px; }";
        MessageBody += "                .col2 { width: 400px; padding: 3px; border: solid #EEEEEE 1px; }";
        MessageBody += "                .head { width: 500px; padding: 3px; border: solid #EEEEEE 1px; text-align: left; }";
        MessageBody += "                .msg  { width: 500px; padding: 3px; border: solid #EEEEEE 1px; }";
        MessageBody += "            </style>";
        MessageBody += "        </head>";
        MessageBody += "    <body>";
        MessageBody += "        <p>A visitor to jacceramics.com has sent you a message.</p>";
        MessageBody += "        <table class='grid'>";
        MessageBody += "            <tr>";
        MessageBody += "                <th class='head' colspan='2'>Contact</th>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>Name:</td>";
        MessageBody += "                <td class='col2'>" + Name.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>Email:</td>";
        MessageBody += "                <td class='col2'>" + EmailAddress.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>Telephone:</td>";
        MessageBody += "                <td class='col2'>" + TelephoneNumber.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <th class='head' colspan='2'>Address</th>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>Street:</td>";
        MessageBody += "                <td class='col2'>" + StreetAddress.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>City:</td>";
        MessageBody += "                <td class='col2'>" + City.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>State/Province:</td>";
        MessageBody += "                <td class='col2'>" + State.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>Zip/Postal:</td>";
        MessageBody += "                <td class='col2'>" + Zip.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='col1'>Country:</td>";
        MessageBody += "                <td class='col2'>" + Country.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <th class='head' colspan='2'>Message</th>";
        MessageBody += "            </tr>";
        MessageBody += "            <tr>";
        MessageBody += "                <td class='msg' colspan='2'>" + MessageBox.Text + "</td>";
        MessageBody += "            </tr>";
        MessageBody += "        </table>";
        MessageBody += "    </body>";
        MessageBody += "</html>";

        return MessageBody;    
    }
}
